package com.starry.push.netty.websocket;

import com.starry.push.netty.config.AppConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * @author WuChao
 * @version 1.0
 * @date 2020/11/3 11:51
 */
@Component
@Slf4j
public class NettyBoot implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private AppConfig appConfig;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

        if (contextRefreshedEvent.getApplicationContext().getParent() == null) {
            //事件获得上下文对象化后启动服务器
            try {
                WebSocketServer.getInstance()
                        .start(appConfig.getWsPort());
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }

    }
}
